import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_rider/main/widgets/ui_constant.dart';
import 'package:flutter_rider/utils/screen_fit.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:flutter_rider/generated/r.dart';

class Toast {
  static show(BuildContext context,
      {String ico = R.image_toast_ok, String msg = "操作成功", int seconds = 2}) {
    showDialog(
        context: context,
        barrierColor: UIColor.color_opcity,
        barrierDismissible: false,
        builder: (context) {
          return MyToast(ico, msg, seconds);
        });
  }
}

class MyToast extends Dialog {
  final String ico;
  final String msg;
  final int seconds;

  const MyToast(this.ico, this.msg, this.seconds, {Key? key}) : super(key: key);

  // 添加定时器自动关闭
  _showTimer(context) {
    Timer.periodic(Duration(milliseconds: seconds * 1000), (timer) {
      Navigator.pop(context);
      timer.cancel();
    });
  }

  @override
  Widget build(BuildContext context) {
    // 调用方法
    _showTimer(context);

    return Center(
        child: Container(
            width: 136.rpx,
            height: 130.rpx,
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(8.0),
              color: UIColor.color_t5,
            ),
            child: Column(
              children: [
                27.hspace,
                SvgPicture.asset(ico, width: 42.rpx, height: 42.rpx),
                15.hspace,
                Text(msg,
                    style: const TextStyle(
                      color: Colors.white,
                      fontSize: 14,
                      decoration: TextDecoration.none,
                    )),
              ],
            )));
  }
}
